/**
 * 
 */
package cn.ad.server.web.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * AdServer 生命周期维护服务接口
 * @author Wu Chengjun
 */
public class AdserverLifeCycle {
	
	private static Logger LOGGER = LoggerFactory.getLogger(AdserverLifeCycle.class);
	
	/**
	 * 系统启动列表
	 */
	private List<IAdserverInitialization> initializations;
	
	/**
	 * 系统关闭列表
	 */
	private List<IAdserverDisposable> destroys;
	
	/**
	 * Aserver服务系启动处理方法
	 */
	void init() throws AdserverLifeCycleException{
		if(initializations != null){
			for(IAdserverInitialization init : initializations){
				try {
					init.init();
				} catch (AdserverInitException e) {
					throw new AdserverLifeCycleException("Adserver 初始化服务[" + init.getClass().getName() + "] 时出现异常.", e);
				}
			}
		}
	}
	
	/**
	 * Adserver服务系统关闭时处理方法
	 */
	void destroy() throws AdserverLifeCycleException{
		if(destroys != null){
			for(IAdserverDisposable closer : destroys){
				try {
					closer.destroy();
				} catch (AdserverCloseException e) {
					LOGGER.error("Adserver 关闭服务[" + closer.getClass().getName() + "] 时出现异常.", e);
				}
			}
		}
	}

	/**
	 * @return the initializations
	 */
	public List<IAdserverInitialization> getInitializations() {
		return initializations;
	}

	/**
	 * @param initializations the initializations to set
	 */
	public void setInitializations(List<IAdserverInitialization> initializations) {
		this.initializations = initializations;
	}

	/**
	 * @return the destroys
	 */
	public List<IAdserverDisposable> getDestroys() {
		return destroys;
	}

	/**
	 * @param destroys the destroys to set
	 */
	public void setDestroys(List<IAdserverDisposable> destroys) {
		this.destroys = destroys;
	}
}
